package net.bolue.web.mvc;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.bolue.console.data.InsertInfo;
import net.bolue.console.data.model.InsertInfoModel;
import net.bolue.sysmng.exception.BolueException;
import net.bolue.web.entry.FormTable;
import net.bolue.web.util.FormUtil;

/**
 * 初始化新增页面
 * @author Administrator
 *
 */
public class InitInsertAction extends AbstractAction{

	@Override
	public ActionResult perform(HttpServletRequest request, HttpServletResponse response) throws BolueException{
//		String subSys = RequestUtil.getParamValue(request, RepConstants.REP_SUBSYS_KEY);
//		String insertID = RequestUtil.getParamValue(request, RepConstants.REP_INSERTID_KEY);
//		if(subSys==null || subSys.trim().length()==0 || insertID==null || insertID.trim().length()==0){
//			throw new BolueException(1, "新增URL中必须提供subSys及insertID参数值!");
//		}
		
//		InsResModel insResModel = new InsResModel(request);
//		InsRes insRes = insResModel.getInsRes(subSys, insertID);
//		if(insRes==null){
//			throw new BolueException(10, "没有找到subSys="+subSys+",insertID="+insertID+"的新增记录所用资源！");
//		}
		
		return this.init(request, response);
	}
	
	public ActionResult init(HttpServletRequest request, HttpServletResponse response) throws BolueException{
		InsertInfoModel insertInfoModel = new InsertInfoModel(request);
		InsertInfo insertInfo = insertInfoModel.getInsertInfo();
		FormTable formTable = FormUtil.getFTForInsert(insertInfo);
		Map<String,Object> pageMap = new HashMap<String,Object>();
		pageMap.put("subSys", insertInfo.getSubSys());
		pageMap.put("insertId", insertInfo.getInsertId());
		pageMap.put("formTable", formTable);
		ActionResult actionResult = new ActionResult();
		actionResult.setData(pageMap);
		actionResult.setResult(true);
		return actionResult;
		
		/*
		String subSys = insRes.getSubSys();
		String tableName = insRes.getResObj();
		String colInfoSql = "SELECT * FROM st_colinfo where SUBSYS='"+subSys+"' AND TBLNAME='"+tableName+"' ORDER BY DISPNO";
		List<FormElemTag> elemTagList = new ArrayList<FormElemTag>();
		Map<String,List<FormDataOption>> dicsMap = new HashMap<String,List<FormDataOption>>();
		DbOpUtil dbOpUtil = new DbOpUtil(RepConstants.SYSMNG_SUBSYS_KEY);
		List<ColInfo> colInfoList = dbOpUtil.query(ColInfo.class,colInfoSql, null);
		if(colInfoList!=null){
			String dispCols = insRes.getDispCols();
			if(dispCols!=null&&dispCols.length()>0){
				dispCols = ","+dispCols+",";
			}else{
				dispCols = "";
			}
			for(ColInfo colInfo : colInfoList){
				String colName = colInfo.getColName();
				FormElemTag formElemTag = TagUtil.colToField(colInfo);
				Object initValue = request.getParameter("col_"+colName);
				formElemTag.setInitValue(initValue);
				if(colInfo.getTagType()!=4){
					if(dispCols.matches(".*,"+colName+",.*")){
						formElemTag.setTagType(-1);
					}
				}
				elemTagList.add(formElemTag);
				Integer  dicGrpId = 0;
				if(colInfo.getDicGrpid()!=null){
					dicGrpId = colInfo.getDicGrpid();
				}
				List<FormDataOption> optionList = DictUtil.getOptionOfAllDic(colInfo.getDicKind(), colInfo.getDicSubSys(), dicGrpId);
				if(optionList!=null&&optionList.size()>0){
					dicsMap.put(colInfo.getDicKind()+"_"+colInfo.getDicGrpid(), optionList);
				}
			}
		}
		
		ActionResult actionResult = new ActionResult();
		Map<String,Object> pageMap = new HashMap<String,Object>();
		pageMap.put("elemTagList", elemTagList);
		pageMap.put("dicsMap", dicsMap);
		pageMap.put("insRes", insRes);
		actionResult.setData(pageMap);
		actionResult.setResult(true);
		return actionResult;
		*/
	}

}
